/**
 * 
 */
package cn.com.shiro;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import cn.com.shiro.dao.impl.UserDaoImpl;
import cn.com.shiro.vo.User;

/**
 * @className 
 * @descripion 
 * @author WangJun
 * @date 2018年6月27日上午11:10:16
 */
public class CustomRealm extends AuthorizingRealm {
//	private CredentialsMatcher credentialsMatcher;
	@Autowired
	private UserDaoImpl userDao;
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		// TODO Auto-generated method stub
		String userName = (String)principals.getPrimaryPrincipal();
		Set<String> roles = getRoles(userName);
		Set<String> permission = getPermissions(userName);
		SimpleAuthorizationInfo authorizationinfo = new SimpleAuthorizationInfo();
		authorizationinfo.setRoles(roles);
		authorizationinfo.setStringPermissions(permission);
		return authorizationinfo;
	}

	/**
	 * @param userName
	 * @return
	 */
	private Set<String> getPermissions(String userName) {
		// TODO Auto-generated method stub
		List<String> perms = userDao.queryPermsByName(userName);
		Set<String> set = new HashSet<String>(perms);
		return set;
	}

	/**
	 * @param userName
	 * @return
	 */
	private Set<String> getRoles(String userName) {
		// TODO Auto-generated method stub
		List<String> roles = userDao.queryRolesByName(userName);
		Set<String> set = new HashSet<String>(roles);
		return set;
	}

	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		// TODO Auto-generated method stub
		String username = (String)token.getPrincipal();
		String password = getPassWordByUserName(username);
		if(password == null) {
			return null;
		}
		SimpleAuthenticationInfo authentication = new SimpleAuthenticationInfo(username,password,"custonRealm");
		authentication.setCredentialsSalt(ByteSource.Util.bytes("123"));
		return authentication;
	}

	/**
	 * @param username
	 * @return
	 */
	private String getPassWordByUserName(String username) {
		// TODO Auto-generated method stub
		User user = userDao.getUserByName(username);
		if(user!=null) {
			return user.getPassword();
		}
		 
		return null;
	}
	public static void main(String[] args) {
		Md5Hash md5 = new Md5Hash("123456", "zhangsan",1);
		System.out.println(md5);
	}

//	public CredentialsMatcher getCredentialsMatcher() {
//		return credentialsMatcher;
//	}
//
//	public void setCredentialsMatcher(CredentialsMatcher credentialsMatcher) {
//		this.credentialsMatcher = credentialsMatcher;
//	}
//	
}
